package com.example.navigationdemo2

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import com.example.navigationdemo2.databinding.FragmentDetailBinding
import kotlinx.android.synthetic.main.fragment_detail.*


/**
 * A simple [Fragment] subclass.
 * Use the [DetailFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class DetailFragment : Fragment() {


    private var mViewModel:MyViewModel?=null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        //viewmodel的持有者为Acitvity
        mViewModel=ViewModelProvider(activity!!).get(MyViewModel::class.java)
        var binding:FragmentDetailBinding=DataBindingUtil.inflate(inflater,R.layout.fragment_detail,container,false)
        binding.data=mViewModel
        //databing的生命周期也是与Activity一致
        binding.lifecycleOwner=activity
        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
//        var content:String=arguments!!.getString("content","")
//        textView.text=content
        button.setOnClickListener(View.OnClickListener {
            Navigation.findNavController(it)
                .navigate(R.id.action_detailFragment_to_homeFragment)
        })

    }

}
